//
//  YUHomeViewModel.swift
//  tuniu
//
//  Created by 于世民 on 2019/8/15.
//  Copyright © 2019 于世民. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import NSObject_Rx
import RxAlamofire
import SwiftyJSON
import RxDataSources
import ObjectMapper

class YUHomeViewModel: NSObject {
    
    private let vmDatas = Variable<[[YUHomeTopModel]]>([])
    
}

extension YUHomeViewModel: YUViewModelType {
   
    typealias Input = YUHomeInput
    
    typealias Output = YUHomeOutput
    
    struct YUHomeInput {
        
    }
    
    struct YUHomeOutput : OutputRefreshProtocol {
        
        var refreshStatus: Variable<YURefreshStatus>
        
        let sections: Driver<[YUHomeSection]>
        
        //最基础的监听者
        let requestCommand = PublishSubject<Bool>()
        
        init(sections: Driver<[YUHomeSection]>) {
            self.sections = sections
            refreshStatus = Variable<YURefreshStatus>(.none)
        }
    }
    
    func transform(input: YUHomeViewModel.YUHomeInput) -> YUHomeViewModel.YUHomeOutput {
        
        let temp_sections = vmDatas.asObservable().map({(sections) -> [YUHomeSection] in
            print(sections)
            return sections.map({ (models) -> YUHomeSection in
                return YUHomeSection(items: models)
            })
        }).asDriver(onErrorJustReturn: [])
        
        let output = YUHomeOutput(sections: temp_sections)
        
        output.requestCommand.subscribe(onNext: { [weak self] _ in
            guard let `self` = self else { return }
            
            var url: String = "https://api.tuniu.com/portal/home/data/index?d=%7B%22abroadCityCode%22%3A0%2C%22clientModel%22%3A%22OPPO+R9s%22%2C%22did%22%3A%22ODY0MzI0MDM2NDIyNTcz%5Cn%22%2C%22height%22%3A1920%2C%22lat%22%3A39.910833%2C%22lng%22%3A116.456336%2C%22localCityCode%22%3A200%2C%22pin%22%3A0%2C%22policies%22%3A%7B%7D%2C%22uniqueKey%22%3A%22864324036422573%22%2C%22width%22%3A1080%7D&c=%7B%22cc%22%3A200%2C%22ct%22%3A20%2C%22dt%22%3A1%2C%22ov%22%3A20%2C%22p%22%3A14905%2C%22v%22%3A%2210.13.0%22%7D"
            
            let request = json(.get, url)
            
            request.asObservable().subscribe({ (event) in
                switch event {
                case .next(_):
                    break
                case let .error(error):
                    HCLog(error)
                case .completed:
                    break
                }
                output.refreshStatus.value = .endHeaderRefresh
            }).disposed(by: self.rx.disposeBag)
            
            request.hc_json({
                
                return JSON($0)
            }).mapObject(YUHomeTopModel.self).subscribe(onNext: { (data) in
               
                self.vmDatas.value = [[data]]
                
            }).disposed(by: self.rx.disposeBag)
        })
        
        return output
        
    }
}

struct YUHomeSection {
    
    var items: [Item]
}

extension YUHomeSection: SectionModelType {
    typealias Item = YUHomeTopModel
    
    init(original: YUHomeSection, items: [YUHomeTopModel]) {
        self = original
        self.items = items
    }
    
    
}
